<?php
 /*can be called as follows:
 
  createpng.php?start_hour=06&start_minute=45&end_hour=10&end_minute=20&imgheight=30&endtime=0845&name=Matt%20Neilson&type=T
  
 */
 

 error_reporting(E_ALL);
 
 $start = sprintf("%04d",$_GET['start']);
 $end   = sprintf("%04d",$_GET['end']);
 $startHour = substr($start, 0, 2);
 $startMin  = substr($start, 2, 2);
 $endHour   = substr($end, 0, 2);
 $endMin    = substr($end, 2, 2);
 $type      = $_GET['type'];
 $base = 'shifts/';
 $fname = md5($start.$_GET['name'].$end.$type.$_GET['fullsize']) . '.png';
 $filename = $base . $fname;

 /*
  with "half time" enabled, all images are half as short as they would be if 1 pixel represented 1 minute.
 */ 

 if($_GET['fullsize'] == 1) {
   $halftime = false;
 } else {
   $halftime = true;
 }

 //Hard-coded override of halftime
  //$halftime = true;

 if($halftime) {
  $pixels_per_hour = 30;
  $pixels_per_minute = .5;
 } else {
  $pixels_per_hour = 60;
  $pixels_per_minute = 1;
 }



 $img_width = (($endHour - $startHour) * $pixels_per_hour) + ($endMin - $startMin) * $pixels_per_minute;
 $img_height= 30;
 $starttime = $start;
 $endtime = $end;
 $design = 2;

 //create img
 $img = @imagecreatetruecolor($img_width, $img_height); 

 //set background
 if($type == 'O') {
   $background = imagecolorallocate($img, 255, 255, 175); // assign yellow bg for open shifts
 } else if($type == 'E') {
   $background = imagecolorallocate($img, 200, 200, 200); // assign Gray bg for shifts for open shifts without signup auth.
   $type = 'O'; //allow program to continue as though open shift
 } else if($type == 'M') {
   $background = imagecolorallocate($img, 150, 200, 250); // assign blue bg for shifts owned by the current owner
   $type = 'S';  // allow program to continue as though it is a regular shift
 } else {
   $background = imagecolorallocate($img, 255, 255, 255); // assign white for everything else
 } 

 //fill
 imagefill($img, 0, 0, $background);

 //Set color
 $black = imagecolorallocate($img, 0, 0, 0);
 $red   = imagecolorallocate($img, 255, 0, 0);
 $blue  = imagecolorallocate($img, 0, 0, 255);
 $green = imagecolorallocate($img, 0, 255, 50);
 $gray  = imagecolorallocate($img, 150, 150, 150);

 //if just filler or other non-text images, do them first
 if($type == 'F' || $type == 'N') { // if type = filler or type = not-running/shutdown or type= not-eligible
   switch ($type) {
     case 'F':
	   $linecolor = $gray;
	   break;
	 case 'N':
	   $linecolor = $red;
	   break;
   }
   $x_line = 10;
   while($x_line < $img_width + 30) {
     imageline($img,$x_line,0,0,$x_line,$linecolor);
	 $x_line += 10;
   }
 } else if($type == 'X') {//create cross hatching for unavailable shifts
   $x_line = -20;
   while($x_line < $img_width + 30) {
     imageline($img,$x_line,0,0,$x_line,$gray);
	 imageline($img,$x_line,0,$x_line+30,$img_height,$gray);
	 $x_line += 10;
   }
 } else if($type == 'S' || $type == 'O') { // otherwise lets print out the text, etc.  
   //try printing text
   if($starttime > 2399) { //if after midnight
   	$str_starttime = sprintf("%04d",$starttime - 2400);
   } else {
    $str_starttime = $starttime;
   }
   if($endtime > 2399) { //if after midnight
   	$str_endtime = sprintf("%04d",$endtime - 2400);
   } else {
    $str_endtime = $endtime;
   }
   imagestring($img, 1, 2, 2, $str_starttime, $black);
   imagestring($img, 1, $img_width - 22, $img_height - 10, $str_endtime, $black);
   if($type == 'S') {
     switch ($_GET['color']) {
	   case 'R':
	     $namecolor = $red;
		 break;
	   default:
	     $namecolor = $black;
	 }
     imagestring($img, 2, 5, 8, $_GET['name'], $namecolor);
   }
 } //end type if
 //draw lines
 if($design == 1) {
   imageline($img,0,0,0,6,$black);
   imageline($img,0,0,6,0,$black);
   imageline($img,$img_width - 7, $img_height - 1, $img_width - 1, $img_height - 1, $black);
   imageline($img,$img_width -1, $img_height - 7, $img_width-1, $img_height-1, $black);
 } else if($design == 2) {
   //imageline($img,0,0,0,30,$black);
   imageline($img,0,0,0,30,$black);
   //imageline($img,$img_width - 7, $img_height - 1, $img_width - 1, $img_height - 1, $black);
   imageline($img,$img_width -1, 0, $img_width-1, $img_height-1, $black);
 }

 //output the image

 imagepng($img,$filename);

	print "<!-- created image " . $fname . " on the fly! -->";

?>
